package com.jingfu.common;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.NativeWebRequest;
import org.zalando.problem.Problem;
import org.zalando.problem.ThrowableProblem;
import org.zalando.problem.spring.web.advice.AdviceTrait;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/1/29 下午4:15
 **/
public interface UnauthorizedAdviceTrait extends AdviceTrait {
    @ExceptionHandler
    default ResponseEntity<Problem> handleBadRequestProblem(
            final UnauthorizedException problem,
            final NativeWebRequest request) {
        ThrowableProblem build = Problem.builder()
                .withStatus(ErrorCode.UNAUTHORIZED)
                .withDetail("您没有权限访问，请联系管理员！")
                .withTitle(ErrorCode.UNAUTHORIZED.getReasonPhrase())
                .build();
        return create(build, request);
    }
}
